import time

from PySide6.QtCore import QThread

from common import frame_to_timecode
from log_manager import LogManager
from python_get_resolve import GetResolve


class ResolveTest(QThread):
    def __init__(self):
        super().__init__()

    def run(self):
        while True:
            print("test")
            time.sleep(1)


def do_test():
    resolve = GetResolve()
    projectManager = resolve.GetProjectManager()
    # 获取当前打开的项目
    currentProject = projectManager.GetCurrentProject()
    # 输出项目名称
    LogManager.log(f"当前项目: {currentProject.GetName()}")
    # 获取当前打开的时间线

    currentTimeline = currentProject.GetCurrentTimeline()
    fps = currentTimeline.GetSetting('timelineFrameRate')
    LogManager.log(f"当前时间线: {currentTimeline.GetName()}")
    LogManager.log(f"当前时间线: fps {fps}")
    srt_items = currentTimeline.GetItemListInTrack("subtitle", 1)
    LogManager.log(f"当前时间线: {currentTimeline.GetName()}")
    last_item = None
    for item in srt_items:
        LogManager.log(f"字幕项: {item.GetName()}")
        LogManager.log(f"字幕项: start {item.GetStart()}")
        LogManager.log(f"字幕项: end {item.GetEnd()}")
        if last_item:
            if item.GetStart() - last_item.GetEnd() > 1:
                LogManager.log(
                    f"字幕项: {item.GetName()} 与 {last_item.GetName()} 有间隔"
                )
                timecode = frame_to_timecode(item.GetStart(), fps)
                LogManager.log(f"字幕项: {item.GetName()} 时间码: {timecode}")
                currentTimeline.SetCurrentTimecode(timecode)
        last_item = item


if __name__ == "__main__":
    worker = ResolveTest()
    worker.start()
    time.sleep(3)
    worker.terminate()
    worker.wait()
    time.sleep(3)
    worker.start()
    time.sleep(3)
    worker.terminate()
